/**
 *  Copyright (C) 2008-2013  Telosys project org. ( http://www.telosys.org/ )
 *
 *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *          http://www.gnu.org/licenses/lgpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.telosys.persistence;

/**
 * Class for joker character substitution ( e.g. '*' -> '%' in SQL )
 *  
 * @author Laurent GUERIN
 * 
 */
public class Joker
{
    public final static int REPLACE_ALL   = 1;

    public final static int REPLACE_FIRST = 2;

    public final static int REPLACE_LAST  = 3;

    private int             _iReplaceMode = REPLACE_ALL;

    private char            _cUserJoker   = '*';

    private char            _cSqlJoker    = '%';

    //    /**
    //     * Defines a user joker character ( it will be replaced by '%' )
    //     * @param userJoker
    //     */
    //    public Joker(String userJoker)
    //    {
    //        if ( userJoker != null )
    //        {
    //            if ( userJoker.length() > 0 )
    //            {
    //                _cUserJoker = userJoker.charAt(0);
    //            }
    //        }
    //    }

    /**
     * Defines a user joker character ( it will be replaced by '%' )
     * The replace mode is REPLACE_ALL by default 
     * 
     * @param userJoker
     */
    public Joker(char userJoker)
    {
        _cUserJoker = userJoker;
    }

    /**
     * Defines a user joker character ( it will be replaced by '%' )
     * with a specific replace mode 
     * @param userJoker
     * @param replaceMode : REPLACE_ALL or REPLACE_FIRST or REPLACE_LAST
     */
    public Joker(char userJoker, int replaceMode)
    {
        _cUserJoker = userJoker;
        if (replaceMode >= REPLACE_ALL && replaceMode <= REPLACE_LAST)
        {
            _iReplaceMode = replaceMode;
        }
    }

    //    /**
    //     * Defines a user joker character with a specific replacement character
    //     * @param userJoker
    //     * @param sqlJoker
    //     */
    //    public Joker(String userJoker, String sqlJoker)
    //    {
    //        if ( userJoker != null && sqlJoker != null )
    //        {
    //            if ( userJoker.length() > 0 && sqlJoker.length() > 0)
    //            {
    //                _cUserJoker = userJoker.charAt(0);
    //                _cSqlJoker = sqlJoker.charAt(0);
    //            }
    //        }
    //    }

    /**
     * Defines a user joker character with a specific replacement character
     * The replace mode is REPLACE_ALL by default 
     * @param userJoker
     * @param sqlJoker
     */
    public Joker(char userJoker, char sqlJoker)
    {
        _cUserJoker = userJoker;
        _cSqlJoker = sqlJoker;
    }

    /**
     * Defines a user joker character with a specific replacement character
     * and a specific replace mode 
     * @param userJoker
     * @param sqlJoker
     * @param replaceMode : REPLACE_ALL or REPLACE_FIRST or REPLACE_LAST
     */
    public Joker(char userJoker, char sqlJoker, int replaceMode)
    {
        _cUserJoker = userJoker;
        _cSqlJoker = sqlJoker;
        if (replaceMode >= REPLACE_ALL && replaceMode <= REPLACE_LAST)
        {
            _iReplaceMode = replaceMode;
        }
    }

    /**
     * Replaces the user joker(s) by the standard or specific joker character
     * It replaces all, first or last user joker according with the "replace mode"
     * 
     * @param s
     * @return
     */
    public String replace(String s)
    {
        if (_iReplaceMode == REPLACE_FIRST )
        {
            return replaceFirst(s);
        }            
        else if (_iReplaceMode == REPLACE_LAST )
        {
            return replaceLast(s);
        }
        else 
        {
            return replaceAll(s);
        }
    }

    /**
     * Replaces all the user jokers by the standard or specific joker character
     * @param s
     * @return
     */
    public String replaceAll(String s)
    {
        if (s != null)
        {
            return s.replace(_cUserJoker, _cSqlJoker);
        }
        return s;
    }

    /**
     * Replaces the last character of the given string if it's a user joker
     * 
     * @param s
     * @return
     */
    public String replaceLast(String s)
    {
        if (s != null)
        {
            int length = s.length();
            if (length > 0)
            {
                if (s.charAt(length - 1) == _cUserJoker)
                {
                    return s.substring(0, length - 1) + _cSqlJoker;
                }
            }
        }
        return s;
    }

    /**
     * Replaces the first character of the given string if it's a user joker
     * @param s
     * @return
     */
    public String replaceFirst(String s)
    {
        if (s != null)
        {
            int length = s.length();
            if (length > 0)
            {
                if (s.charAt(0) == _cUserJoker)
                {
                    return _cSqlJoker + s.substring(1);
                }
            }
        }
        return s;
    }

    public String toString()
    {
        String s = "REPLACE_ALL";
        if ( _iReplaceMode == REPLACE_FIRST ) s = "REPLACE_FIRST";
        if ( _iReplaceMode == REPLACE_LAST  ) s = "REPLACE_LAST";
        return "Joker : '" + _cUserJoker + "' -> '" + _cSqlJoker + "' (mode=" + _iReplaceMode + ":" + s + ")" ;
    }

}